package com.huawei.esdk.uc.sametime.toolbar;

import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

import com.huawei.esdk.uc.jna.Constants.PhoneJointType;
import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.ConstantsDefines.CallStatus;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.services.UCCallService;
import com.huawei.esdk.uc.sametime.services.UCSettingsService;
import com.huawei.esdk.uc.sametime.toolbar.actions.EsdkPhoneCtlDropDownAction;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.util.ImageFactory;

/**
 * 呼叫之前设置呼出设备的工具栏菜单
 * @author y00175154
 * 
 */
public class ToolbarPhoneCtlCascadeMenuCreator implements IMenuCreator
{

    // private EsdkPhoneCtlDropDownAction phoneCtlAction;

    private Menu menu = null;

    public ToolbarPhoneCtlCascadeMenuCreator(
            EsdkPhoneCtlDropDownAction phoneCtlAction)
    {
        // this.phoneCtlAction = phoneCtlAction;

    }

    public void dispose()
    {
        if (null != menu)
        {
            menu.dispose();
        }
    }

    public Menu getMenu(Control parent)
    {
        if (null != menu)
        {
            menu.dispose();
        }
        menu = new Menu(parent.getShell());
        fillMenu(menu);

        return menu;
    }

    public Menu getMenu(Menu parent)
    {
        if (null != menu)
        {
            menu.dispose();
        }
        menu = new Menu(parent);
        fillMenu(menu);
        return menu;
    }

    private void fillMenu(Menu parent)
    {
        // 使用PC呼叫的菜单项
        MenuItem pcModeMenuItem = new MenuItem(parent, SWT.NONE);
        pcModeMenuItem
                .setText(Messages.ToolbarPhoneCtlCascadeMenuCreator_CALL_WITH_COMPUTER);
        pcModeMenuItem.setImage(ImageFactory.loadImage(parent.getDisplay(),
                ImageFactory.PC_MODE));

        pcModeMenuItem.addSelectionListener(new PCModeSelectionAdpter());

        // 使用IP PHONE呼叫菜单项
        MenuItem phoneModeMenuItem = new MenuItem(parent, SWT.NONE);
        phoneModeMenuItem
                .setText(Messages.ToolbarPhoneCtlCascadeMenuCreator_CALL_WITH_TEL_DEVICE);
        phoneModeMenuItem.setImage(ImageFactory.loadImage(parent.getDisplay(),
                ImageFactory.PHONE_MODE));
        phoneModeMenuItem
                .addSelectionListener(new IPPhoneModeSelectionAdpter());

        if(CallStatus.end != UCCallService.getCallStatus())
        {
            pcModeMenuItem.setEnabled(false);
            phoneModeMenuItem.setEnabled(false);
            return;
        }
        
        // 如果是当前呼叫设备是ip phone,则选择pc设备菜单项可用,选择iphone设备菜单项不可用,否则就反过来
        int currentCallDevice = GlobalConfiguration.getInstance()
                .getCallDevice();
        if (PhoneJointType.IPPhone_Device == currentCallDevice)
        {
            pcModeMenuItem.setEnabled(true);
            phoneModeMenuItem.setEnabled(false);
        }
        else
        {
            //如果当前的呼叫设备类型不是IP话机，则认为是PC
            pcModeMenuItem.setEnabled(false);
            phoneModeMenuItem.setEnabled(true);
        }
    }

    private static class PCModeSelectionAdpter extends SelectionAdapter
    {
        @Override
        public void widgetSelected(SelectionEvent e)
        {
            // 这里调用UC SDK的切换软终端呼叫模式服务接口
            boolean res_switch = UCSettingsService
                    .setDefaultOutgoingCallDev(ConstantsDefines.PC_MODE);
            if (!res_switch)
            {
                MessageDialog
                        .openError(
                                null,
                                Messages.ERROR_TIP,
                                Messages.ToolbarPhoneCtlCascadeMenuCreator_ERROR_CHANGE_SOFT_TERMINAL_FAIL);
            }
        }
    }

    private static class IPPhoneModeSelectionAdpter extends SelectionAdapter
    {
        @Override
        public void widgetSelected(SelectionEvent e)
        {
            // 这里调用UC SDK的切换IP Phone呼叫模式服务接口
            boolean res_switch = UCSettingsService
                    .setDefaultOutgoingCallDev(ConstantsDefines.IP_PHONE_MODE);
            boolean isIPPhoneEnabled = UCSettingsService.isIPPhoneEnabled();
            if (!res_switch)
            {
                MessageDialog
                        .openError(
                                null,
                                Messages.ERROR_TIP,
                                Messages.ToolbarPhoneCtlCascadeMenuCreator_CHANGE_IPPHONE_FAIL);
            }
            if (!isIPPhoneEnabled)
            {
                MessageDialog
                        .openError(
                                null,
                                Messages.ERROR_TIP,
                                Messages.ToolbarPhoneCtlCascadeMenuCreator_BINDING_CALL_DEVICE);
            }
        }
    }
}
